package com.gzy.cbrsystem.core.insomnia;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


import com.gzy.cbrsystem.core.ReasoningResult;
import com.gzy.cbrsystem.core.SortBySim;
import com.gzy.cbrsystem.core.interfaces.CaseBasedReaoner;
import com.gzy.cbrsystem.core.interfaces.CaseBasic;
import com.gzy.cbrsystem.core.interfaces.ReasoningMethod;
import com.gzy.cbrsystem.core.interfaces.SimilarityCalculation;

public class InsomniaReasoner implements CaseBasedReaoner {

	InsomniaCaseBase caseBase;
	SimilarityCalculation similarityCalculation;
	ReasoningMethod reasoningMethod;
	public InsomniaReasoner()
	{
		//this.caseBase.initCaseBase();
	}
	public SimilarityCalculation getSimilarityCalculation() {
		return similarityCalculation;
	}



	public void setSimilarityCalculation(SimilarityCalculation similarityCalculation) {
		this.similarityCalculation = similarityCalculation;
	}



	public ReasoningMethod getReasoningMethod() {
		return reasoningMethod;
	}



	public void setReasoningMethod(ReasoningMethod reasoningMethod) {
		this.reasoningMethod = reasoningMethod;
	}



	public void init() {
		caseBase.initCaseBase();
	}

	public InsomniaCaseBase getCaseBase() {
		return caseBase;
	}

	public void setCaseBase(InsomniaCaseBase caseBase) {
		this.caseBase=caseBase;

	}

	public List<ReasoningResult> Query(CaseBasic query) {
		List<ReasoningResult> reResults=reasoningMethod.calculateSimilarity(query, caseBase, this.similarityCalculation);
		return reResults;
	}



	public List<ReasoningResult> reason(CaseBasic query, int K) {
		this.init();
		List<ReasoningResult> reResults=Query(query);
		Collections.sort(reResults, new SortBySim());
		List<ReasoningResult> topK=new ArrayList();
		Iterator<ReasoningResult> reIter  =reResults.iterator(); 
		for(int i=0; i<K && i<reResults.size(); i++)
		    topK.add(reIter.next());
		return topK;
	}

}
